package com.example.design.patterns.冰河.结构型模式.享元模式.flyWeightFactory;

import com.example.design.patterns.冰河.结构型模式.享元模式.concreteFlyWeight.FlyWeightImpl;
import com.example.design.patterns.冰河.结构型模式.享元模式.flyWeight.FlyWeight;
import java.util.HashMap;
import java.util.Map;

/**
 * FlyWeightFactory 创建并管理 flyWeight对象.确保合理的共享 flyWeight。
 * 当用户请求一个flyWeight时，FlyWeightFactory对象提供一个已创建的实例或者创建一个（如果不存在的话）。
 */
public class FlyWeightFactory {

    private static Map flyWeights = new HashMap();

    public FlyWeightFactory(String arg) {
        flyWeights.put(arg, new FlyWeightImpl());
    }

    public static FlyWeight getFlyWeight(String key) {
        if (flyWeights.get(key) == null) {
            flyWeights.put(key, new FlyWeightImpl());
        }
        return (FlyWeight) flyWeights.get(key);
    }

    public static int getSize() {
        return flyWeights.size();
    }
}
